﻿Imports System.IO.IsolatedStorage
Imports System.IO
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Runa
Imports Runa.Common.Helper
Imports Runa.UI.Input
Imports Runa.Resources.ResourceManager
Imports Runa.Resources.ContentManagers
Imports Runa.Core.Services
Imports Runa.Foundation
Imports Runa.Foundation.Diagnostics
Imports Runa.Foundation.Messaging
Imports Runa.Foundation.Runtime
Imports Runa.Foundation.Threading
Imports Runa.Scripting.ScriptingHost
Imports Runa.Core.GamePackage
Imports Runa.Foundation.Runtime.Serialization

#If (Not NETFX_CORE) And (Not WINDOWS_PHONE) Then
Imports System.Windows.Forms
#Else
#If WINDOWS_PHONE Then
Imports System.Windows
#End If
#End If

Partial Public Class GamePage
    Inherits PhoneApplicationPage

    Public WithEvents SystemSpriteBatch As SpriteBatch

    Public ReadOnly Property Game() As IGame
        Get
            Return CType(Me.m_gameRender, IGame)
        End Get
    End Property

    Private contentManager As ContentManager
    Private timer As GameTimer
    Private spriteBatch As SpriteBatch

    Private m_gamePackage As GamePackageBase
    Private m_gameRender As GameRender

    Public Sub New()
        InitializeComponent()

        '' Get the content manager from the application
        'contentManager = DirectCast(Application.Current, App).Content

        '' Create a timer for this page
        'timer = New GameTimer()
        'timer.UpdateInterval = TimeSpan.FromTicks(333333)
        'AddHandler timer.Update, AddressOf OnUpdate
        'AddHandler timer.Draw, AddressOf OnDraw
    End Sub

    Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
        Try
            Dim parameters = Me.NavigationContext.QueryString

            If parameters.ContainsKey("GameName") Then
                Me.m_gamePackage = g_gameList.Where(Function(obj) obj.Name = Uri.UnescapeDataString(parameters("GameName")))(0)
                Me.m_gameRender = New GameRender(Me.m_gamePackage)
            Else
                Throw New ArgumentException("GameName parameter is missing.")
            End If

            ' Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(True)

            ' TODO: use Me.content to load your game content here
            Me.m_gameRender.Graphics.ApplyChanges()

            Me.Game.Initialize()
            Me.Game.LoadContent()

            ' Start the timer
            timer.Start()
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace.ToString())
        End Try
        
        MyBase.OnNavigatedTo(e)
    End Sub

    Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
        ' Stop the timer
        timer.Stop()

        Me.Game.UnloadContent()

        ' Set the sharing mode of the graphics device to turn off XNA rendering
        SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(False)

        MyBase.OnNavigatedFrom(e)
    End Sub

    ''' <summary>
    ''' Allows the page to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    Private Sub OnUpdate(sender As Object, e As GameTimerEventArgs)
        ' TODO: Add your update logic here
        Me.Game.Update(New GameTime(e.TotalTime, e.ElapsedTime))
    End Sub

    ''' <summary>
    ''' Allows the page to draw itself.
    ''' </summary>
    Private Sub OnDraw(sender As Object, e As GameTimerEventArgs)
        SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue)
        ' TODO: Add your drawing code here
        Me.Game.Draw(New GameTime(e.TotalTime, e.ElapsedTime))
    End Sub

End Class